package cn.com.scitc.loginservlet;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebFilter(filterName = "ManagerFilter",urlPatterns = "/manager/*")
public class ManagerFilter implements Filter {
    public void destroy() {
        
    }
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest)req;
        HttpServletResponse response = (HttpServletResponse)resp;

        String uri = request.getRequestURI();
//        System.out.println("当前访问的url是：" + uri);

        final HttpSession session = request.getSession();
        if (session.getAttribute("currentUser") == null){
            //没有登录
            if (uri.contains("/manager/login") || uri.contains("/manager/checkLogin") || uri.contains("/manager/test")){
                //放开
            }else {
                //进行过滤，过滤掉home
                //硬代码，写法不好
                response.sendRedirect("/webshool/manager/login");
                return;
            }
        }else {
            //已经登录
            //完全放开
        }

        chain.doFilter(req, resp);
    }

    public void init(FilterConfig config) throws ServletException {

    }

}
